﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core.Interfaces;
using Cofe.Core.Property;

namespace Cofe.Core.EntryLinks
{
    public class EntryLinkPrimaryPP : PropertyProviderBase, IEntryLinkPrimaryPP
    {
        #region Constructor

        public EntryLinkPrimaryPP(IContainerBehavior parentContainerBehavior, string parentPath, string label, string targetPath)
        {
            ParentPath = parentPath;
            Label = label;
            TargetPath = targetPath;
            //Console.WriteLine("EntryLinkPrimaryPP: {0}\\{1}={2}", ParentPath, Label, TargetPath);
            ParentContainerBehavior = parentContainerBehavior;

            registerProperties(CofeProperties.ParentParseName, () => ParentPath);
            registerProperties(CofeProperties.FullParseName, () => ParsePath);
            registerProperties(CofeProperties.Label, () => Label);
            registerProperties(CofeProperties.Name, () => Label, (pp) => { ParentContainerBehavior.Rename(Label, pp.ValueAsString); Label = pp.ValueAsString; return true; });
            registerProperties(CofeProperties.CofeAttributes, () => CofeAttributes.VirtualItems);
            registerProperties(CofeProperties.DefaultInterfaceType, () => typeof(IEntryLink));
            registerProperties(CofeProperties.Type, () => EntryTypes.Link);

            registerProperties(CofeEntryLinkProperties.LinkedEntryPH, () =>
                CofeServices.PathParserManager.Behaviors.TryParsePath(TargetPath, ParsableType.ParsePathLookup));
                      
            registerProperties(CofeProperties.Position, () => OverridePosition.HasValue ? OverridePosition.Value : ParentContainerBehavior.GetPosition(Label),
                (pp) => { ParentContainerBehavior.SetPosition(Label, pp.ValueAsInt32); return true; });
            registerAction(CofeEntryLinkProperties.DeleteLink, (pm) => { ParentContainerBehavior.Remove(Label); pm.IsHandled = true; });
        }
        
        #endregion

        #region Methods
        
        #endregion

        #region Data
        
        #endregion

        #region Public Properties

        internal IContainerBehavior ParentContainerBehavior { get; private set; }
        public string ParentPath { get; private set; }
        public string Label { get; private set; }
        public string TargetPath { get; private set; }
        public int? OverridePosition { get; set; }
        public string ParsePath
        {
            get { return PathFE.Combine(ParentPath, Label); }
        }
        
        #endregion

      
    }
}
